package com.team.weigou.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RadioButton;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.team.weigou.R;
import com.team.weigou.adapter.HotRecommenAdapter;
import com.team.weigou.adapter.HotSearchAdapter;
import com.team.weigou.bean.Data;
import com.team.weigou.bean.DataEntity;
import com.team.weigou.config.UrlConfig;
import com.team.weigou.ui.HotInfoActivity;
import com.team.weigou.ui.HotSearchActivity;
import com.team.weigou.ui.SearchActivity;
import com.team.weigou.widget.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class HotFragment extends Fragment implements AdapterView.OnItemClickListener{
    @ViewInject(R.id.search_rb)
    private RadioButton search_rb;
    @ViewInject(R.id.hot_RQ_gradView)
    private MyGridView rqGridView;
    @ViewInject(R.id.hot_RS_gradView)
    private MyGridView rsGridView;
    private HotSearchAdapter hotSearchAdapter;
    private HotRecommenAdapter hotRecommenAdapter;
    private ArrayList<Data> list = new ArrayList<>();
    private ArrayList<DataEntity> list2 = new ArrayList<>();
    private BitmapUtils bitmapUtils;
    private HttpUtils httpUtils = new HttpUtils();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBitmUtils();
    }

    /**
     * 初始化BitmapUtils工具类
     */
    private void initBitmUtils() {
        bitmapUtils = new BitmapUtils(getActivity());
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_empty);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_empty);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot_layout, container, false);
        ViewUtils.inject(this, view);
        search_rb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
        initView();
        initData();
        initData2();
        return view;
    }

    private void initView() {
        hotSearchAdapter = new HotSearchAdapter(getActivity(), list, bitmapUtils);
        hotRecommenAdapter = new HotRecommenAdapter(getActivity(), list2, bitmapUtils);

        rsGridView.setOnItemClickListener(this);
        rqGridView.setOnItemClickListener(this);

        rsGridView.setAdapter(hotSearchAdapter);
        rqGridView.setAdapter(hotRecommenAdapter);

    }

    private void initData() {
        RequestParams params = getRequestParams();

        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConfig.HOT_SEARCH_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonResult = responseInfo.result;
                try {
                    String msg = new JSONObject(jsonResult).optString("msg");
                    if ("获取成功".equals(msg)) {
                        JSONArray arr = new JSONObject(jsonResult).getJSONArray("data");
                        List<Data> datas = JSON.parseArray(arr.toString(), Data.class);
                        list.addAll(datas);
                        hotSearchAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }

    private void initData2() {
        RequestParams params = getRequestParamR();

        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConfig.HOT_RECOMMEN_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonResult = responseInfo.result;
                try {
                    String msg = new JSONObject(jsonResult).optString("msg");
                    if ("获取成功".equals(msg)) {
                        JSONArray arr = new JSONObject(jsonResult).getJSONArray("data");
                        List<DataEntity> datas = JSON.parseArray(arr.toString(), DataEntity.class);
                        list2.addAll(datas);
                        hotRecommenAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("pageSize", "20");
        params.addBodyParameter("pageNo", "1");
        params.addBodyParameter("pid", "0");

        return params;
    }

    private RequestParams getRequestParamR() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("pageSize", "28");
        params.addBodyParameter("pageNo", "1");

        return params;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.hot_RS_gradView:
                Intent intent = new Intent(getActivity(), HotSearchActivity.class);
                intent.putExtra("name", list.get(position).getName() + "");
                intent.putExtra("pid", list.get(position).getId() + "");
                startActivity(intent);
                break;
            case R.id.hot_RQ_gradView:
                Intent intents = new Intent(getActivity(), HotInfoActivity.class);
                intents.putExtra("name", list2.get(position).getName() + "");
                startActivity(intents);
                break;
        }
    }

}
